package com.mcxx.modules.miniSalvation.order.service.write;

import cn.hutool.core.util.IdUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dto.FuneralDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.entity.FuneralEntity;
import com.mcxx.modules.miniSalvation.order.dao.write.FuneralWriteDao;
import com.mcxx.modules.miniSalvation.order.service.read.FuneralReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCertReadService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@TaTransactional
public class FuneralWriteService {

    @Autowired
    private FuneralWriteDao funeralWriteDao;
    @Autowired
    private FuneralReadService funeralReadService;
    @Autowired
    private OrderCertReadService orderCertReadService;


    public int insert(FuneralEntity funeral) {
    	return funeralWriteDao.insert(funeral);
    }


    public int insertIgnoreNull(FuneralEntity funeral) {
    	return funeralWriteDao.insertIgnoreNull(funeral);
    }


    public int update(FuneralEntity funeral) {
    	return funeralWriteDao.update(funeral);
    }


    public int updateNew(FuneralEntity funeral) {
        return funeralWriteDao.updateNew(funeral);
    }


    public int updateIgnoreNull(FuneralEntity funeral) {
    	return funeralWriteDao.updateIgnoreNull(funeral);
    }


    public int delete(FuneralEntity funeral) {
    	return funeralWriteDao.delete(funeral);
    }


    public void modifyFuneralExpense(FuneralDTO funeralDTO,UserAccountVo accountVo) {

        if(Constant.recevierType.PERSONAL.equals(funeralDTO.getRecevierType())) {
            this.verifyOrderCaregiver(funeralDTO);
        }

        FuneralEntity funeralEntity = new FuneralEntity();
        BeanUtils.copyProperties(funeralDTO,funeralEntity);
        funeralEntity.setUpdateTime(new Date());
        funeralEntity.setHandleDate(new Date());
//        funeralEntity.setOperator(accountVo.getUserId());
        funeralEntity.setApprover(accountVo.getUsername());
        funeralEntity.setOperator(accountVo.getUserId());


        updateNew(funeralEntity);
    }


    public void addFuneralExpense(FuneralDTO funeralDTO, UserAccountVo curUserAccount) {

        this.verifyOrderCaregiver(funeralDTO);


        if (Constant.DomesticRelation.ONESELF.equals(funeralDTO.getFamilyRelation())) {
            throw new BizException(FailureCode.ERR_100017);
        }


        FuneralEntity entity = funeralReadService.getByMemberId(funeralDTO.getMemberId());
        if (entity != null) {
            throw new BizException(FailureCode.ERR_10002);
        }


        entity = new FuneralEntity();
        BeanUtils.copyProperties(funeralDTO,entity);
        entity.setApprover(curUserAccount.getUsername());
        entity.setOperator(curUserAccount.getUserId());
//        entity.setId(IdUtil.simpleUUID());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setStatus("01");
        entity.setBusinessType(Constant.BusinessCode.TEKUN);


        funeralWriteDao.insertIgnoreNull(entity);
    }

    public void verifyOrderCaregiver(FuneralDTO funeralDTO){

        if(Constant.recevierType.PERSONAL.equals(funeralDTO.getRecevierType())){

            int rstIdcard= orderCertReadService.cntCertNum(funeralDTO.getId(),funeralDTO.getOrderId(),"102","12");


            if(rstIdcard <= 0){
                throw new BizException("-1", "当前信息没有上传【身份证照片】，请上传！");
            }

            int rstBank = orderCertReadService.cntCertNum(funeralDTO.getId(),funeralDTO.getOrderId(),"401","12");

            if(rstBank <= 0){
                throw new BizException("-1", "当前信息没有上传【银行卡照片】，请上传！");
            }

            int rst = orderCertReadService.cntCertNum(funeralDTO.getId(),funeralDTO.getOrderId(),"105","12");

            if(rst <= 0){
                throw new BizException("-1", "当前信息没有上传【丧葬费用证明材料】，请上传！");
            }
        } else {

            int rst = orderCertReadService.cntCertNum(funeralDTO.getId(),funeralDTO.getOrderId(),"105","12");

            if(rst <= 0){
                throw new BizException("-1", "当前信息没有上传【丧葬费用证明材料】，请上传！");
            }
        }

    }



    public void updateAduit(List<String> ids,String status) {


        funeralWriteDao.updateStatusById(ids,status);
    }
}
